#!/bin/bash
set -e

NGINX_URL="$1"   # 反向代理，如xxx:8080
NGINX_PORT="$2"  # 监听端口，如8000
UMIRC_URL="$3"	 # 转发地址，如xxx:8080
ECS_IP='120.27.221.71'

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
PROJECT_ROOT=$(dirname ${SCRIPT_DIR})
cd ${PROJECT_ROOT}

echo ">> 删除工程目录..."
ssh root@${ECS_IP} "rm -rf /root/flowplatform-fe"
echo ">> git下载工程..."
ssh root@${ECS_IP} "cd /root && git clone git@code.dayu.work:flowplatform-water/flowplatform-fe.git"
echo ">> 切换dev分支并更新..."
ssh root@${ECS_IP} "cd /root/flowplatform-fe && git checkout -b dev origin/dev && git pull"
echo ">> 授予脚本执行权限并启动docker..."
ssh root@${ECS_IP} "chmod +x /root/flowplatform-fe/docker/bin/*.sh && systemctl start docker"
echo ">> 覆盖安装项目依赖，防止找不到依赖..."
ssh root@${ECS_IP} "cd /root/flowplatform-fe && cnpm set registry https://registry-cnpm.dayu.work/ && cnpm install --registry=https://registry-cnpm.dayu.work/"
echo ">> 修改default.conf和umirc.js转发地址..."
ssh root@${ECS_IP} "cd /root/flowplatform-fe && bash ./dockerfe/beforestart.sh ${NGINX_URL} ${NGINX_PORT} ${UMIRC_URL}"
echo ">> 编译项目..."
ssh root@${ECS_IP} "cd /root/flowplatform-fe && npm run buildFlow"
echo ">> 打包docker..."
ssh root@${ECS_IP} "cd /root/flowplatform-fe && bash ./dockerfe/build.sh flowfe $(date +%Y%m%d)"  # 以日期(年月日)为后缀作为版本号
echo ">> 运行docker..."
ssh root@${ECS_IP} "cd /root/flowplatform-fe && bash ./dockerfe/start.sh flowfe $(date +%Y%m%d)"  # 以日期(年月日)为后缀作为版本号
echo ">> 删除工程目录..."
ssh root@${ECS_IP} "rm -rf /root/flowplatform-fe"
echo ">> 全部完成！"
